﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;

using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Task。
    /// </summary>
    public class TaskDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(Task model)
        {
            using (Entities db = new Entities())
            {
                db.Tasks.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Task model)
        {
            using (Entities db = new Entities())
            {
                db.Tasks.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Tasks where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Tasks.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Task GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.Tasks.Where(x => x.Id == Id).FirstOrDefault();
            }
        }
        #endregion

        #region 任务列表
        
        /// <summary>
        /// 任务列表
        /// </summary>
        /// <returns></returns>
        public IList<Task> DataList()
        {
            using (Entities db = new Entities())
            {
                return db.Tasks.ToList();
            }
        }
        #endregion

        #region 任务列表
        
        /// <summary>
        /// 任务列表
        /// </summary>
        /// <returns></returns>
        public IList<Task> GetUsedList()
        {
            using (Entities db = new Entities())
            {
                return db.Tasks.Where(x => x.IsUsed).ToList();
            }
        }
        #endregion

        #region 更新最新执行的时间
        /// <summary>
        /// 更新最新执行的时间
        /// </summary>
        /// <returns></returns>
        public void UpdateLastRunTime(int Id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Tasks.Where(x => x.Id == Id).First();
                q.LastRunTime = DateTime.Now;

                db.SaveChangesAsync().Wait();
            }
        }
        
        #endregion

    }
}